/*
 * Copyright (C) 2011 Olivier Pothin and contributors
 *
 * GNU General Public License v2.0
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/**
 * 
 * @author Olivier Pothin
 * @description Activity displaying all the words known by the application
 */
package opot.activities;

import opot.dto.Word;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class Dictionary extends Activity implements OnClickListener{

	public static final String DICTIONARY_INITIAL_SELECTED= "___INITALSELECTEDDICTIONNARY";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.dictionary);
		findViewById(R.id.menubouton).setOnClickListener(this);
		registerForContextMenu(findViewById(R.id.menubouton));

		Cursor c = managedQuery(Word.Words.CONTENT_URI, null, null, null, Word.Words._ID+" asc");

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.dico_item, c, 
				new String[]{Word.Words.COLUMN_NAME_FRENCH,Word.Words.COLUMN_NAME_CHINESE,Word.Words.COLUMN_NAME_PINYIN},
				new int[]{R.id.dico_fr,R.id.dico_zh,R.id.dico_pi});

		

		ListView v = (ListView) findViewById(R.id.dicolist);
		v.setAdapter(adapter);
		//FIXME Utiliser les actions plutot que lexextras,avec des URI
		if(getIntent().getExtras() != null) {
			//Log.d("DICO","Position : "+getIntent().getExtras().getInt(DICTIONARY_INITIAL_SELECTED));

			v.setSelectionFromTop(getIntent().getExtras().getInt(DICTIONARY_INITIAL_SELECTED)-1, 0);
		}
		
		registerForContextMenu(v);
		
		//GoogleAnalyticsTracker.getInstance().trackPageView("/Dictionnaire");
	}

	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.menubouton:
			openContextMenu(v);
			break;
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		switch (v.getId()) {
		case R.id.menubouton:
			menu.setHeaderIcon(R.drawable.icon);
			inflater.inflate(R.menu.mcontextmenu, menu);
			break;
		case R.id.dicolist:
			inflater.inflate(R.menu.deletewordmenu, menu);
			break;
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Intent intent;
		
		switch(item.getItemId()){
		//on ne gere pas dico car on y est deja :)
		case R.id.menu_wordisplay:
			intent = new Intent(this,WordDisplayer.class);
			startActivity(intent);
			break;
		case R.id.to_add_word_view:
			intent = new Intent(this,WordManagement.class);
			startActivity(intent);
			
		case R.id.delete_word_item_menu:
			
			if( item.getMenuInfo() instanceof AdapterContextMenuInfo) {
				
			}
			
			break;
			
		default:
			break;
		}

		return super.onContextItemSelected(item);
	}

	@Override
	protected void onStop() {
		//GoogleAnalyticsTracker.getInstance().dispatch();
		super.onStop();
	}
}
